<?php

class Workset_Controller_Action_Helper_HttpUri extends Zend_Controller_Action_Helper_Abstract {
    /**
     * @param null $uri
     * @param null $scheme
     * @return Zend_Uri_Http
     */
    function direct($uri = null, $scheme = null) {
        return $this->getUri($uri, $scheme);
    }

    /**
     * @param null $uri
     * @param null $scheme
     * @return Zend_Uri_Http
     */
    function getUri($uri = null, $scheme = null) {
        if (empty($uri)) {
        	$uri = $this->getFrontController()->getRequest()->getRequestUri();
        }
        $uri = trim($uri, '\/');

        $scheme = is_string($scheme) ? $scheme : 'http';

        if (false === strpos($uri, $scheme.'://')) {
            $domain = $this->getFrontController()->getRequest()->getHttpHost();
            $domain = preg_replace("/^www\./iu", "", $domain);
            if (!preg_match("/^(www\.)?".$domain."/iu", $uri)) {
                $uri = $domain.'/'.$uri;
            }
            $uri = $scheme.'://'.$uri;
        }

        return Zend_Uri_Http::fromString($uri);
    }
}
